package oa.hleast.xswl.service.chat.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.mapper.chat.ChatAutoReplyMapper;
import oa.hleast.xswl.pojo.domain.chat.CmsChatAutoReply;
import oa.hleast.xswl.pojo.form.chat.CmsChatAutoReplyForm;
import oa.hleast.xswl.pojo.vo.chat.CmsChatAutoReplyVo;
import oa.hleast.xswl.service.chat.IChatAutoReplyService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 302
 */
@Service
@Slf4j
@Transactional
@AllArgsConstructor
public class ChatAutoReplyServiceImpl extends ServiceImpl<ChatAutoReplyMapper, CmsChatAutoReply> implements IChatAutoReplyService {

    @Override
    public boolean add(CmsChatAutoReplyForm chatAutoReplyForm) {

        CmsChatAutoReply chatAutoReply = conversionToPojo(chatAutoReplyForm);

        return this.save(chatAutoReply);
    }

    @Override
    public boolean updateReply(CmsChatAutoReplyForm chatAutoReplyForm) {
        //校验ID
        CmsChatAutoReply cmsAppsIconOld = this.getById(chatAutoReplyForm.getId());
        if (ObjectUtils.isEmpty(cmsAppsIconOld)) {
            throw new BizException("没有找到该数据！");
        }

        CmsChatAutoReply cmsAppsIcon = new CmsChatAutoReply();
        BeanUtil.copyProperties(chatAutoReplyForm, cmsAppsIcon);
        cmsAppsIcon.setId(cmsAppsIconOld.getId());

        return this.updateById(cmsAppsIcon);
    }

    @Override
    public Page<CmsChatAutoReplyVo> pageListByApps(Integer page, Integer limit) {
        LambdaQueryWrapper<CmsChatAutoReply> queryWrapper = new LambdaQueryWrapper<CmsChatAutoReply>()
                .orderByAsc(CmsChatAutoReply::getGmtModified)
                .orderByAsc(CmsChatAutoReply::getGmtCreate);


        Page<CmsChatAutoReply> cmsAppsIconPage = this.page(new Page<>(page, limit), queryWrapper);

        if (cmsAppsIconPage.getTotal() == 0) {
            return new Page<>(page, limit);
        }

        List<CmsChatAutoReplyVo> cmsAppsIconList = cmsAppsIconPage.getRecords().stream().map(this::conversionToVo).collect(Collectors.toList());

        Page<CmsChatAutoReplyVo> voPage = new Page<>(page, limit, cmsAppsIconPage.getTotal());
        voPage.setRecords(cmsAppsIconList);


        return voPage;
    }

    @Override
    public CmsChatAutoReplyVo detail(Long id) {
        CmsChatAutoReply chatAutoReply = this.getById(id);

        CmsChatAutoReplyVo vo = conversionToVo(chatAutoReply);

        return vo;
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        if (CollectionUtils.isFull(asList)) {
            throw new BizException("没有选择任何数据，请重新操作！");
        }

        return this.removeByIds(asList);
    }

    /**
     * ===========  私有方法 ===========
     */
    private CmsChatAutoReplyVo conversionToVo(CmsChatAutoReply pojo) {
        if (ObjectUtils.isEmpty(pojo)) {
            return new CmsChatAutoReplyVo();
        }

        CmsChatAutoReplyVo vo = new CmsChatAutoReplyVo();
        BeanUtil.copyProperties(pojo, vo);

        return vo;
    }

    private CmsChatAutoReply conversionToPojo(CmsChatAutoReplyForm chatAutoReplyForm) {
        if (ObjectUtils.isEmpty(chatAutoReplyForm)) {
            throw new BizException("提交的数据表单为空");
        }

        CmsChatAutoReply cmsAppsIcon = new CmsChatAutoReply();
        BeanUtil.copyProperties(chatAutoReplyForm, cmsAppsIcon);

        return cmsAppsIcon;
    }
}
